package com.css.fxfzfxqh.modules.beyondProbability.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.common.rest.model.bo.JcsjDataViewBo;
import com.css.fxfzfxqh.common.rest.model.bo.JcsjVersionPageBo;
import com.css.fxfzfxqh.common.rest.model.param.JcsjDataViewParam;
import com.css.fxfzfxqh.modules.beyondProbability.dto.BeyondProVersionPageParam;
import com.css.fxfzfxqh.modules.beyondProbability.service.BeyondProbabilityService;
import com.css.fxfzfxqh.modules.beyondProbability.vo.ParamsVo;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.exceptions.BizException;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/beyondProbability")
@Slf4j
public class BeyondProbabilityController {

    @Autowired
    BeyondProbabilityService beyondProbabilityService;

    /**
     * 超越概率地震危险性
     * 获取版本号和数据级别   用接口从基础数据中获取
     *
     * @return
     */
    @GetMapping("/version")
    public RestResponse getVersion(String num) {
        RestResponse response = null;
        try {
            JSONObject version = beyondProbabilityService.getVersion(num);
            response = RestResponse.succeed(version);
        } catch (Exception e) {
            log.error("获取列表信息失败!", e);
            response = RestResponse.fail("获取列表信息失败!");
        }
        return response;
    }

    /**
     * 查询版本
     *
     * @param param
     * @return
     */
    @GetMapping("versionPage")
    public RestResponse<?> versionPage(BeyondProVersionPageParam param) {
        try {
            JcsjVersionPageBo r = beyondProbabilityService.versionPage(param);
            if (r.getCode() == 200) {
                return RestResponse.succeed(r.getData());
            } else {
                return RestResponse.fail(r.getMessage());
            }
        } catch (BizException e) {
            return RestResponse.fail(e.getMessage());
        } catch (Exception e1) {
            log.error(e1.getMessage(), e1);
            return RestResponse.fail("查询失败");
        }
    }

    /**
     * 查询数据视图接口
     *
     * @param param 查询参数
     * @return
     * @throws BizException
     */
    @GetMapping("queryDataView")
    public RestResponse<?> queryDataView(JcsjDataViewParam param) {
        try {
            JcsjDataViewBo r = beyondProbabilityService.queryDataView(param);
            if (r.getCode() == 200) {
                return RestResponse.succeed(r.getData());
            } else {
                return RestResponse.fail(r.getMessage());
            }
        } catch (BizException e) {
            return RestResponse.fail(e.getMessage());
        } catch (Exception e1) {
            log.error(e1.getMessage(), e1);
            return RestResponse.fail("查询失败");
        }
    }

    /**
     * 超越概率地震危险性
     * 调用接口从基础数据中获取数据
     *
     * @param paramsVo
     * @param request
     * @return
     */
    @GetMapping("/page")
    public RestResponse page(ParamsVo paramsVo, HttpServletRequest request) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject page = beyondProbabilityService.page(paramsVo, curPage, pageSize);
            response = RestResponse.succeed(page);
        } catch (Exception e) {
            log.error("获取列表信息失败!", e);
            response = RestResponse.fail("获取列表信息失败!");
        }
        return response;
    }
}
